package com.foreverwin.jzymes.production.labor.controller;


import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.production.labor.domain.LaborAttendanceLog;
import com.foreverwin.jzymes.production.labor.service.ILaborAttendSerchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Date;

@RestController
@RequestMapping("/production/labor")
public class LaborAttanceLogController extends BaseController {
    @Autowired
    ILaborAttendSerchService serchService;


    /**新增人力出勤信息*/
    @RequestMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> jsonMap){
       try {
           LaborAttendanceLog attendanceLog = new LaborAttendanceLog();

           attendanceLog.setSite( CommonMethods.getSite() );
           attendanceLog.setShift( (String)jsonMap.get("shift") );
           attendanceLog.setDept( (String)jsonMap.get("deptBo") );
           attendanceLog.setProcedureBo( BOHandleUtil.buildFieldGroupHandle( CommonMethods.getSite(),(String)jsonMap.get("procedure") ) );
           attendanceLog.setActualLabor( Long.valueOf( (String)jsonMap.get("actualLabor") ) );
           attendanceLog.setStdLabor( Long.valueOf( (String)jsonMap.get("stdLabor") ) );
           Date workDate = null;
           if ( (String)jsonMap.get( "workDate" ) != null || (String)jsonMap.get( "workDate" ) != "" ){
               SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
               try {
                   workDate = simpleDateFormat.parse( (String)jsonMap.get( "workDate" ) );
               } catch (ParseException e) {
                   e.printStackTrace();
               }
           }
           attendanceLog.setWorkDate( workDate );
           attendanceLog.setCreateUserBo( CommonMethods.getUserBo() );

           serchService.insertLaborAttend( attendanceLog );
           return  AjaxResult.returnSuccess();
       }catch (BusinessException e){
           return  AjaxResult.returnError(e.getMessage());
       }
    }


}
